<?php
/**
 * 
 * Front контроллер новостей
 * @author andrew
 *
 */
class News_IndexController extends Zend_Controller_Action {
	/**
	 * 
	 * Вывод всех новостей
	 */
	public function indexAction() {
		//создание фильтра
		$form = new News_Form_FrontTools();
		$form->setAction($this->view->url(array(),'news_index_index'));
        
        for($i = 2011; $i<=date("Y"); $i++) {
        	$yearMultiOptions[$i] = $i;
        }
        $year = $form->getElement('year');
        $year->addMultiOptions($yearMultiOptions);
        $year->setValue(date("Y"));
		
        if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
        	$values = $form->getValues();
        	
        	$month = $values['month'];
        	$year = $values['year'];
        	
        	
        	$start_time = mktime(null,null,null,$month,1,$year);
        	$end_time = mktime(null,null,null,$month,date("t",mktime(null,null,null,$month,null,$year)),$year);
        	
        	if((int)$month == 0) {
        		$start_time = mktime(null,null,null,1,1,$year);
        		$end_time = mktime(null,null,null,12,31,$year);
        	}
        	
        	$news = Doctrine_Query::create()
				->from('News_Model_News')
				->orderBy('created_at DESC')
				->where('created_at > ?', $start_time)
				->andWhere('created_at < ?', $end_time)
				->execute();
        } else {
       
			$news = Doctrine_Query::create()
					->from('News_Model_News')
					->orderBy('created_at DESC')
					->execute();
        }
		
		//устананавливаем метатеги для главной страницы
		$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		if($setting) {
			$this->view->headMeta()->appendName('description', $setting->Translation[$this->view->LANG]->seo_description);
			$this->view->headMeta()->appendName('keywords', $setting->Translation[$this->view->LANG]->seo_keywords);
		}
		
		$this->view->headTitle($this->view->translate('NEWS'));
		$this->view->form = $form;
		$this->view->news = $news;
	}
	
	public function feedAction() {
		//получаем последние 10 записей
		$news = Doctrine_Query::create()
					->from('News_Model_News')
					->orderBy('created_at DESC')
					->limit(10)
					->execute();
		
		$feedTitle = $this->view->SITENAME;
		$feedLink = $this->view->STATIC_SERVER;
		
		//описание
		$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		if($setting) {
			$description = $setting->Translation[$this->view->LANG]->seo_description;
		} else {
			$description = $this->view->translate('RSS Feed');
		}
		
		$feedArray = array(
            'title'       => $feedTitle,
            'link'        => $feedLink,
            'description' => $description,
            'charset'     => 'UTF-8',
            'entries'     => array()
        );
        
        foreach ($news as $item) {
        	$feedArray['entries'][] = array(
                'title'       => $item->Translation[$this->view->LANG]->name,
                'link'        => $feedLink . '/' . $this->view->LANG . '/news/' . $item->id,
                'description' => $item->Translation[$this->view->LANG]->anonce,
                'lastUpdate'  => $item->created_at
            );
        }
        
        $this->_helper->viewRenderer->setNoRender(true);
        Zend_Layout::getMvcInstance()->disableLayout();
        
        $feed = Zend_Feed::importArray($feedArray, 'rss');
        
        $feed->send();
	}
	/**
	 * 
	 * Вывод одной новости
	 * @throws Zend_Controller_Action_Exception
	 */
	public function showAction() {
		$id = $this->_request->getParam('id',0);
		
		$news = Doctrine_Core::getTable('News_Model_News')->find($id);
		
		if(!$id || !$news)
			throw new Zend_Controller_Action_Exception('News not found',404);
		
		$otherNews = Doctrine_Query::create()
					->from('News_Model_News')
					->orderBy('created_at DESC')
					->where('id <> ?', $id)
					->limit(5)
					->execute();	
		
		//устананавливаем метатеги
		$this->view->headTitle($news->Translation[$this->view->LANG]->name);
		$this->view->headMeta()->appendName('description', $news->Translation[$this->view->LANG]->description);
		$this->view->headMeta()->appendName('keywords', $news->Translation[$this->view->LANG]->keywords);
		
		$this->view->otherNews = $otherNews;
		$this->view->news = $news;
	}
}